/* ***** BEGIN LICENSE BLOCK *****
 * Version: NPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Netscape Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/NPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is lutsko.com code.
 *
 * The Initial Developer of the Original Code is 
 * James Lutsko.
 * Portions created by the Initial Developer are Copyright (C) 2003
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the NPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the NPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

package autoteam.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.URL;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

import login.ui.LoginFrame;
import autoschedule.ui.FileHistory;
import autoteam.League;
import autoteam.Local;
import autoteam.ui.tabs.AutoTeamTabs;
import controller.RSMSController;
import db.ColumnData;
import db.DBRecord;
import db.DataAccessObject;
import java.awt.LayoutManager;
import ui.BackgroundPanel;

/**
 * AutoTeamDeskTop2 is the top-level window of the AutoTeam application. It
 * contains the main function, sets up the menus and provides the menu
 * callbacks.
 * 
 * @author Jim Lutsko
 * @version %Revision$, $Date: 2003/12/02 17:54:07 $
 * @since 1.0
 */
public class AutoTeamDeskTop2 extends JFrame implements ActionListener, FileHistory.IFileHistory
{
  AutoTeamTabs tabsPane = null;
  League doc = null;
  FileHistory fileHistory = null;
  
  private JLabel theTitle;
  private JPanel logo;
  private BackgroundPanel BGpanel;// Logo for the SportsSelectionUI
  private LayoutManager layout = new FlowLayout( FlowLayout.CENTER, 0, 50 );
  private JPanel bottomPanel; // Icon in the bottom right

  private final int frameWidth = 640;
  private final int frameHeight = 480;
  private final Dimension frameSize = new Dimension(frameWidth, frameHeight);
  
  private Toolkit sizer = Toolkit.getDefaultToolkit(); 
  private Dimension screenSize = sizer.getScreenSize();
  
  private final int screenCenterX = (screenSize.width-frameWidth)/2;
  private final int screenCenterY = (screenSize.height-frameHeight)/2;
  

  /*
   * Selection for Sport
   */
  int selected;

  JMenu fileMenu = null;
  /**
   * We keep the filechooser so that it can be re-used.
   */
  JFileChooser fc = null;
  /**
   * The menu items for the file menu. The menu callback determines the menu
   * item chosen by comparing the passed object to these guys.
   */
  JMenuItem fileOpen, fileImport, fileSave, fileSaveAs, fileClose, fileExit, fileLogout;

  /**
   * The menu items for the Options menu. The menu callback determines the menu
   * item chosen by comparing the passed object to these guys.
   */
  JMenuItem clearLog, saveLog, changeLeague;

  /**
   * The menu items for the Help menu. The menu callback determines the menu
   * item chosen by comparing the passed object to these guys.
   */
  JMenuItem helpHelp, helpAbout;

  /**
   * The about box - so we don't have to create it more than once.
   */
  JDialog aboutBox = null;

  /**
   * The help box - so we don't have to create it more than once.
   */
  JDialog helpBox = null;

  /*
   * RSMSController initialization
   */
  RSMSController controller = null;

  /**
   * The sole constructor. The desktop is initialized and the menus built.
   */
  public AutoTeamDeskTop2()
  {
    super(Local.get("ProgramName"));

    BGpanel = new BackgroundPanel("./trunk/img/Background.png");
    logo = new JPanel();
    logo.setOpaque(false);
    this.setContentPane(BGpanel);
    this.setLayout(layout);
    this.add(logo);
    
    this.setSize(frameSize);
    this.setMinimumSize(frameSize);
    this.setLocation(screenCenterX, screenCenterY);
    
    // Create the menu bar.
    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);

    // Build the first menu.
    JMenu menu = new JMenu(Local.get("File"));
    fileMenu = menu;
    menu.setMnemonic(KeyEvent.VK_F);
    menuBar.add(menu);

//    fileOpen = new JMenuItem(Local.get("Open"), KeyEvent.VK_O);
//    fileOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
//        ActionEvent.ALT_MASK + ActionEvent.SHIFT_MASK));
//    fileOpen.addActionListener(this);
//    menu.add(fileOpen);

    fileImport = new JMenuItem(Local.get("Import"), KeyEvent.VK_I);
    fileImport.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I,
        ActionEvent.ALT_MASK + ActionEvent.SHIFT_MASK));
    fileImport.addActionListener(this);
    menu.add(fileImport);

    fileSave = new JMenuItem(Local.get("Save"), KeyEvent.VK_S);
    fileSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
        ActionEvent.ALT_MASK + ActionEvent.SHIFT_MASK));
    fileSave.addActionListener(this);
    fileSave.setEnabled(false);
    menu.add(fileSave);

    fileSaveAs = new JMenuItem(Local.get("SaveAs"), KeyEvent.VK_A);
    fileSaveAs.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,
        ActionEvent.ALT_MASK + ActionEvent.SHIFT_MASK));
    fileSaveAs.addActionListener(this);
    fileSaveAs.setEnabled(false);
    menu.add(fileSaveAs);
    fileSave.setEnabled(false);

    fileLogout = new JMenuItem("Logout");
    fileLogout.addActionListener(this);
    fileLogout.setEnabled(true);
    menu.add(fileLogout);

    menu.addSeparator();

    /*
     * fileClose = new JMenuItem(Local.get("Close"),KeyEvent.VK_C);
     * fileClose.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_C,
     * ActionEvent.ALT_MASK + ActionEvent.SHIFT_MASK));
     * fileClose.addActionListener(this); menu.add(fileClose);
     */

    fileExit = new JMenuItem(Local.get("Exit"), KeyEvent.VK_X);
    fileExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
        ActionEvent.ALT_MASK + ActionEvent.SHIFT_MASK));
    fileExit.addActionListener(this);
    menu.add(fileExit);

    // Build the second menu.
    menu = new JMenu(Local.get("Options"));
    menu.setMnemonic(KeyEvent.VK_O);
    menuBar.add(menu);

    clearLog = new JMenuItem(Local.get("Clear"), KeyEvent.VK_C);
    clearLog.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L,
        ActionEvent.ALT_MASK + ActionEvent.SHIFT_MASK));
    clearLog.addActionListener(this);
    clearLog.setEnabled(false);
    menu.add(clearLog);

    saveLog = new JMenuItem("Save Log", KeyEvent.VK_C);
    saveLog.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L,
        ActionEvent.ALT_MASK + ActionEvent.SHIFT_MASK));
    saveLog.addActionListener(this);
    saveLog.setEnabled(false);
    menu.add(saveLog);

    changeLeague = new JMenuItem(Local.get("ChangeLeague"), KeyEvent.VK_L);
    changeLeague.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G,
        ActionEvent.ALT_MASK + ActionEvent.SHIFT_MASK));
    changeLeague.addActionListener(this);
    changeLeague.setEnabled(false);
    menu.add(changeLeague);

    // Build the help menu
    menu = new JMenu(Local.get("Help"));
    menu.setMnemonic(KeyEvent.VK_H);
    menuBar.add(menu);

    helpHelp = new JMenuItem(Local.get("Help"), KeyEvent.VK_H);
    helpHelp.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H,
        ActionEvent.ALT_MASK + ActionEvent.SHIFT_MASK));
    helpHelp.addActionListener(this);
    menu.add(helpHelp);

    helpAbout = new JMenuItem(Local.get("About"), KeyEvent.VK_A);
    helpAbout.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,
        ActionEvent.ALT_MASK + ActionEvent.SHIFT_MASK));
    helpAbout.addActionListener(this);
    menu.add(helpAbout);

    /* add toggle button to the right of the menu bar */
    menuBar.add(Box.createHorizontalGlue());
    JButton toggleButton = new JButton("Switch to Scheduler >>");
    toggleButton.addActionListener(new ActionListener()
    {

      public void actionPerformed(ActionEvent e)
      {
        controller.toggle();
      }
    });
    menuBar.add(toggleButton);

    // Setup the file history
    //
    fileHistory = new FileHistory(this); // init FileHistory with our frame as
                                         // the only parameter.
    fileHistory.initFileMenuHistory(); // Start file history initialization.

    // Setup the exit method
    //
    this.addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent e)
      {
        exit();
      }
    });

    setVisible(true);
  }

  public AutoTeamDeskTop2(RSMSController newController)
  {
    this();
    this.controller = newController;
  }

  /**
   * Close the frame, save the file history entries, and exit.
   */
  private void exit()
  {
    setVisible(false);
    dispose();
    fileHistory.saveHistoryEntries(); // Save pathname entries for next session.
    System.exit(0);
  }

  /**
   * setParent sets the RSMSController for this JFrame
   * 
   * @author Colin Watson
   * @version %Revision$, $Date: 2010/10/17 2:40 $
   */

  public void setParent(RSMSController newController)
  {
    this.controller = newController;
  }

  /**
   * If doc is dirty, checks whether user wants to proceed
   */

  private boolean checkDoc()
  {
    if (doc == null)
      return false;
    if (doc.isDirty())
    {
      int ret = JOptionPane.showConfirmDialog(
              null,
              "You have unsaved changes which will be lost if you proceed. Continue?",
              "Proceed?", JOptionPane.YES_NO_OPTION);
      System.out.println("ret = " + ret + " JOptionPane.NO_OPTION  = " + JOptionPane.NO_OPTION);
      if (ret == JOptionPane.NO_OPTION)
        return true;
    }
    return false;
  }

  // ////////////////////////////////////////////////////////////////////////////////////
  // Implement the IFileHistory interface
  //
  // ////////////////////////////////////////////////////////////////////////////////////

  /**
   * Get the application name to identify the configuration file in the the
   * USER_HOME directory. This name should be unique in this directory.
   * 
   * @return the application name.
   */
  public String getApplicationName()
  {
    return Local.get("ProgramName");
  }

  /**
   * Get a handle to the frame's File menu.
   * 
   * @return the frame's File menu.
   */
  public JMenu getFileMenu()
  {
    return fileMenu;
  }

  /**
   * Return the size of the main application frame. It is used to center the
   * File History maintenance window.
   * 
   * @return the main GUI frame's size.
   */
  public Dimension getSize()
  {
    return super.getSize();
  }

  /**
   * Return the main application frame. It is used to center the file history
   * maintenance window.
   * 
   * @return the main GUI frame instance.
   */
  public JFrame getFrame()
  {
    return this;
  }

  /**
   * Perform the load file activity.
   * 
   * @param path
   *          is the pathname of the loaded file.
   */
  public void loadFile(String pathname)
  {
    // doOpen(new File(pathname));
    openFile(new File(pathname));
  }

  // ////////////////////////////////////////////////////////////////////////////////////

  /**
   * Creates a new internal window and adds to desktop.
   * 
   * @param d
   *          The AutoTeamDoc that is being displayed by this window.
   * @return void.
   */
  AutoTeamTabs addWindow(League _doc, File sourceFile)
  {
    doc = _doc;
    tabsPane = null;
    try
    {
      tabsPane = new AutoTeamTabs(doc,controller.getActiveFrame());
      tabsPane.setVisible(true); // necessary as of 1.3

      if (theTitle == null)
        theTitle = new JLabel("", JLabel.CENTER);
      theTitle.setText("<html><h2>" + "Editing League : " + doc.league + "</h2></html>");

      addBottomPanel();

      JPanel p = new JPanel(new BorderLayout());
      p.add(theTitle, BorderLayout.NORTH);
      p.add(tabsPane, BorderLayout.CENTER);
      p.add(bottomPanel, BorderLayout.SOUTH);
      // p.add(toggleButtonPanel,BorderLayout.NORTH);
      setContentPane(p);

      // fileSave.setEnabled(true);
      fileSaveAs.setEnabled(true);
      clearLog.setEnabled(true);
      saveLog.setEnabled(true);
      changeLeague.setEnabled(true);
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    return tabsPane;
  }

  /**
   * Displays the file chooser, pipes the file through the Parser and
   * initializes the skillOrder field of Virtue. It also writes the map file.
   * 
   * @param d
   *          The AutoTeamDoc that is being displayed by this window.
   * @return void.
   */
  void importData()
  {
    if (checkDoc())
      return;
    if (fc == null)
      fc = new JFileChooser(System.getProperty("user.dir"));
    fc.addChoosableFileFilter(new AutoTeamFileFilter("csv",
            "CSV files (*.csv)"));
    fc.addChoosableFileFilter(new AutoTeamFileFilter("xls",
        "MS Excel files (*.xls)"));
    int returnVal = fc.showOpenDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION)
    {
      File file = fc.getSelectedFile();
      openFile(file);
    }
  }

  private void openFile(File file)
  {
	  
    // get the extension
    String ext = null;
    String s = file.getName();
    int i = s.lastIndexOf('.');
    if (i > 0 && i < s.length() - 1)
    {
      ext = s.substring(i + 1).toLowerCase();
    }
    System.out.println("ext = " + ext);
    // process
    if (ext.matches("csv"))
      openCSV(file);
    //else if (ext.matches("xls"))
      //openXLS(file);
    else if (ext.matches("rtb"))
      openRTB(file);
  }

  private void openCSV(File file) {		// changed to the following
	  
	  try
	  {
	      doc = new League(file);
	      //populate teams from teame table
	      
	      DataAccessObject dao = new DataAccessObject();
	      List<DBRecord> teamRecs = dao.getAllRecords("teams");
	      for (int i = 0; i < teamRecs.size(); i++)
	      {
	    	  doc.addTeam((String)teamRecs.get(i).getFieldValue("teamName"));
	    	  /*
	    	  DBRecord theCoach = dao.getRecord("coaches", 
	    			  new ColumnData("id",teamRecs.get(i).getFieldValue("coachId")));
	    	  doc.getTeam(i).coach = (String) theCoach.getFieldValue("firstname");
	    	  */
	    	  
	      }
	      
	      fileSave.setEnabled(false);
	      tabsPane = addWindow(doc, file);
	      // fileHistory.insertPathname(file.getAbsolutePath()); // Hook into
                                                              // FileHistory class
	      RSMSController.resizeActiveFrame(this, false);
	  } catch (Exception e) {
	      e.printStackTrace();
	  }
  }
  
  /*private void openCSV(File file)
  {
    try
    {
      DataMap dm = ParserDialog.showDialog(this, file);
      if (dm != null)
      {
        doc = new League(file, dm);
        fileSave.setEnabled(false);
        tabsPane = addWindow(doc, file);
        //fileHistory.insertPathname( file.getAbsolutePath() );
        controller.resizeActiveFrame(this, false);
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }*/

  /*private void openXLS(File file)
  {
    try
    {
      // choose the worksheet
      Workbook workbook = Workbook.getWorkbook(file);
      int n = workbook.getNumberOfSheets();
      String[] sheets = new String[n];
      for (int i = 0; i < n; i++)
        sheets[i] = workbook.getSheet(i).getName();

      Object sel = JOptionPane.showInputDialog(null, "Choose the worksheet",
          "Worksheet Selection", JOptionPane.INFORMATION_MESSAGE, null, sheets,
          sheets[0]);
      if (sel == null)
        return;

      Sheet sheet = workbook.getSheet(sel.toString());
      DataMap dm = ParserDialog.showDialog(this, sheet, file);
      if (dm != null)
      {
        doc = new League(file, sheet, dm);
        fileSave.setEnabled(false);
        tabsPane = addWindow(doc, file);
        fileHistory.insertPathname(file.getAbsolutePath()); // Hook into
                                                            // FileHistory
                                                            // class
        controller.resizeActiveFrame(this, true);
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }*/

  /**
   * Displays the file chooser to select an AutoTeamDoc file. If selection is
   * successful, reads the object and adds a window.
   * 
   * @return void.
   */
  void openData()
  {
    if (checkDoc())
      return;
    if (fc == null)
      fc = new JFileChooser(System.getProperty("user.dir"));
    fc.addChoosableFileFilter(new AutoTeamFileFilter("rtb",
        "RSMS Team Builder (*.rtb)"));
    int returnVal = fc.showOpenDialog(this);

    if (returnVal == JFileChooser.APPROVE_OPTION)
    {
      File file = fc.getSelectedFile();
      openRTB(file);
    }
  }

  public void openRTB(File file)
  {
    try
    {
      FileInputStream in = new FileInputStream(file);
      ObjectInputStream s = new ObjectInputStream(in);
      doc = (League) s.readObject();
      // AutoTeamTabs frame = addWindow(doc, file);
      doc.HarmonizeNights();
      doc.sourceFile = file;

      fileHistory.insertPathname(file.getAbsolutePath()); // Hook into
                                                          // FileHistory class
      fileSave.setEnabled(true);
      RSMSController.resizeActiveFrame(this, false);
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  void saveData()
  {
    if (doc != null)
    {
      if (doc.sourceFile.getName().endsWith(".rtb"))
      {
        doc.saveDoc(doc.sourceFile);
        JOptionPane.showMessageDialog(null, "League " + doc.league
            + " saved to file " + doc.sourceFile.getAbsolutePath(),
            "File saved ...", JOptionPane.INFORMATION_MESSAGE, null);
      }
      else
        saveDataAs();
    }
    else
      JOptionPane.showMessageDialog(null, Local.get("NoSelectionWin"), Local
          .get("Error"), JOptionPane.ERROR_MESSAGE, null);
  }

  void saveDataAs()
  {
    try
    {
      if (tabsPane == null)
        JOptionPane.showMessageDialog(null, Local.get("NoSelectionWin"), Local
            .get("Error"), JOptionPane.ERROR_MESSAGE, null);
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }

    if (fc == null)
      fc = new JFileChooser(System.getProperty("user.dir"));
    fc.addChoosableFileFilter(new AutoTeamFileFilter("rtb",
        "RSMS Team Builder files (*.rtb)"));
    if (doc.sourceFile != null)
      fc.setSelectedFile(doc.sourceFile);
    else
      fc.setSelectedFile(new File(doc.getLeagueName() + ".rtb"));
    int returnVal = fc.showSaveDialog(this);

    if (returnVal == JFileChooser.APPROVE_OPTION)
    {
      File file = fc.getSelectedFile();
      if (!file.getName().endsWith(".rtb"))
      {
        file = new File(file.getPath() + ".rtb");
      }
      doc.sourceFile = file;
      doc.saveDoc(file);
      JOptionPane.showMessageDialog(null, "League " + doc.league
          + " saved to file " + file.getAbsolutePath(), "File saved ...",
          JOptionPane.INFORMATION_MESSAGE, null);
      fileHistory.insertPathname(file.getAbsolutePath()); // Hook into
                                                          // FileHistory class
      // frame.setTitle(file.getName() + " : " + frame.doc.league);
    }

  }

  void closeWindow()
  {
  /*
   * if(frame != null) frame.dispose(); else
   * JOptionPane.showMessageDialog(null,Local.get("NoSelectionWin"),
   * Local.get("Error"), JOptionPane.ERROR_MESSAGE,null);
   */
  }

  void doClearLog()
  {
    try
    {
      if (tabsPane != null)
        tabsPane.doc.logDocument.remove(0, tabsPane.doc.logDocument.getLength() - 1);
      else
        JOptionPane.showMessageDialog(null, Local.get("NoSelectionWin"), Local
            .get("Error"), JOptionPane.ERROR_MESSAGE, null);
    }
    catch (Exception e)
    {
      ;
    }
  }

  void doSaveLog()
  {
    try
    {
      if (tabsPane != null)
      {
        File file = tabsPane.doc.writeLog();
        if (file != null)
          JOptionPane.showMessageDialog(null, "Log saved to file "
              + file.getAbsolutePath(), "File saved ...",
              JOptionPane.INFORMATION_MESSAGE, null);
      }
      else
        JOptionPane.showMessageDialog(null, Local.get("NoSelectionWin"), Local
            .get("Error"), JOptionPane.ERROR_MESSAGE, null);
    }
    catch (Exception e)
    {
      ;
    }
  }

  void doChangeLeague()
  {
//    try
//    {
//      if (tabsPane != null)
//      {
//        tabsPane.changeLeague();
//        theTitle.setText("<html><h2>" + "Editing League : " + tabsPane.doc.league + "</h2></html>");
//        // setTitle(sourceFile.getName() + " : " + doc.league);
//      }
//      else
//        JOptionPane.showMessageDialog( null, 
//                                       Local.get("NoSelectionWin"),
//                                       Local.get("Error"), 
//                                       JOptionPane.ERROR_MESSAGE, null
//                                      );
//    }
//    catch (Exception e)
//    {
//      ;
//    }
  }

  void doHelp()
  {
    if (helpBox == null)
    {
      JPanel panel = new JPanel();
      panel.setOpaque(false);
      panel.setLayout(new BorderLayout());

      // create a URL object for the Help file
      JEditorPane editorPane = null;
      String s = null;
      try
      {
        s = "jar:file:" + System.getProperty("java.class.path") + "!/"
            + "rsms_tb_help.html";
        URL helpURL = new URL(s);
        try
        {
          editorPane = new JEditorPane(helpURL);
        }
        catch (IOException e)
        {
          System.err.println("Attempted to read a bad URL: " + helpURL);
        }
      }
      catch (Exception e)
      {
        System.err.println("Couldn't create help URL: " + s);
      }
      editorPane.setEditable(false);
      editorPane.addHyperlinkListener(new HyperlinkListener()
      {
        public void hyperlinkUpdate(HyperlinkEvent e)
        {
          if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
          {
            JEditorPane pane = (JEditorPane) e.getSource();
            try
            {
              pane.setPage(e.getURL());
            }
            catch (Throwable t)
            {
              t.printStackTrace();
            }
          }
        }
      });

      JScrollPane editorScrollPane = new JScrollPane(editorPane);
      editorScrollPane
          .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
      editorScrollPane.setPreferredSize(new Dimension(500, 300));

      helpBox = new JDialog(this, Local.get("AboutBoxTitle"), false);
      helpBox.getContentPane().setLayout(new BorderLayout());
      helpBox.getContentPane().add(editorScrollPane, BorderLayout.CENTER);

      JPanel buttonpanel = new JPanel();
      buttonpanel.setOpaque(true);
      JButton button = (JButton) buttonpanel.add(new JButton(Local.get("ok_button_text")));

      helpBox.getContentPane().add(buttonpanel, BorderLayout.SOUTH);
      button.addActionListener(new OkAction(helpBox));
    }
    helpBox.pack();
    Point p = this.getLocationOnScreen();
    helpBox.setLocation(p.x + this.getWidth() / 2 - helpBox.getWidth() / 2, p.y
        + this.getHeight() / 2 - helpBox.getHeight() / 2);
    helpBox.show();
  }

  void doLogout()
  {
    controller.replaceActiveFrameAndLogout(new LoginFrame(controller));
  }

  class OkAction extends AbstractAction
  {
    JDialog aboutBox;

    protected OkAction(JDialog aboutBox)
    {
      super("OkAction");
      this.aboutBox = aboutBox;
    }

    public void actionPerformed(ActionEvent e)
    {
      aboutBox.setVisible(false);
    }
  }

  void doAbout()
  {
    if (aboutBox == null)
    {
      // JPanel panel = new JPanel(new BorderLayout());
      // JPanel panel = new AboutPanel(swingset);
      JPanel panel = new JPanel();
      panel.setOpaque(false);
      panel.setLayout(new BorderLayout());

      JLabel label = new JLabel(Local.get("AboutText"));
      panel.add(label, BorderLayout.CENTER);

      aboutBox = new JDialog(this, Local.get("AboutBoxTitle"), false);
      aboutBox.getContentPane().setLayout(new BorderLayout());
      aboutBox.getContentPane().add(panel, BorderLayout.CENTER);

      JPanel buttonpanel = new JPanel();
      buttonpanel.setOpaque(true);
      JButton button = (JButton) buttonpanel.add(new JButton(Local
          .get("ok_button_text")));
      panel.add(buttonpanel, BorderLayout.SOUTH);

      button.addActionListener(new OkAction(aboutBox));
    }
    aboutBox.pack();
    Point p = this.getLocationOnScreen();
    aboutBox.setLocation(p.x + this.getWidth() / 2 - aboutBox.getWidth() / 2,
        p.y + this.getHeight() / 2 - aboutBox.getHeight() / 2);
    aboutBox.show();
  }

  private void addBottomPanel()
  {
    // Added Bottom Panel
    bottomPanel = new JPanel();
    FlowLayout layout = new FlowLayout(FlowLayout.RIGHT);
    bottomPanel.setLayout(layout);

    JLabel sportsText = new JLabel();
    JLabel sportsIcon = new JLabel();

    if (selected == 1)
    {
      sportsText.setText("<html><h2>" + "Soccer" + "</h2></html>");
      ImageIcon sportsBall = new ImageIcon("./trunk/img/Soccer_48.png");
      sportsIcon.setIcon(sportsBall);
    }
    else if (selected == 2)
    {
      sportsText.setText("<html><h2>" + "Baseball" + "</h2></html>");
      ImageIcon sportsBall = new ImageIcon("./trunk/img/Baseball_48.png");
      sportsIcon.setIcon(sportsBall);
    }
    else if (selected == 3)
    {
      sportsText = new JLabel("<html><h2>" + "Basketball" + "</h2></html>");
      ImageIcon sportsBall = new ImageIcon("./trunk/img/Basketball_48.png");
      sportsIcon.setIcon(sportsBall);
    }
    else if (selected == 4)
    {
      sportsText = new JLabel("<html><h2>" + "Football" + "</h2></html>");
      ImageIcon sportsBall = new ImageIcon("./trunk/img/Football_48.png");
      sportsIcon.setIcon(sportsBall);
    }
    else
    {
      // something weird happened
    }
    bottomPanel.add(new JLabel("<html><h2>" + "RSMS" + "</h2></html>"));
    bottomPanel.add(sportsText);
    bottomPanel.add(sportsIcon);
  }

  public void change_logo(String location, int num)
  {
    ImageIcon icon = new ImageIcon(location);
    JLabel sp = new JLabel();
    sp.setIcon(icon);
    logo.add(sp);
    selected = num;
  }

  public void actionPerformed(ActionEvent e)
  {
    JMenuItem source = (JMenuItem) (e.getSource());
    if (source == fileOpen)
      openData();
    else if (source == fileImport)
      importData();
    else if (source == fileSave)
      saveData();
    else if (source == fileSaveAs)
      saveDataAs();
    // else if(source == fileClose)
    // closeWindow();
    else if (source == fileLogout)
      doLogout();
    else if (source == fileExit)
    {
      if (checkDoc())
        return;
      exit();
    }
    else if (source == clearLog)
      doClearLog();
    else if (source == saveLog)
      doSaveLog();
    else if (source == changeLeague)
      doChangeLeague();
    else if (source == helpHelp)
      doHelp();
    else if (source == helpAbout)
      doAbout();

  }
}
